package com.chenl.provider.DEMOS.日志相关.全链路日志以及Spring线程池装饰器的使用;

import cn.hutool.core.io.IoUtil;
import com.alibaba.fastjson.JSONObject;
import com.chenl.provider.DTO.vo.Result;
import com.chenl.provider.feign.ConFeign;
import com.chenl.provider.feign.ConFeign2;
import feign.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.nio.charset.Charset;


@Slf4j
@RestController
@RequestMapping("/logger")
public class ProLogController {
    @Autowired
    ConFeign conFeign;
    @Autowired
    ConFeign2 conFeign2;

    @Autowired
    LogService logService;

    @Autowired
    ThreadPoolTaskExecutor  threadPoolTaskExecutor;
    /**
     * 一、traceId的跨服务和跨线程传递
     * 1.请求进来时,HandlerInterceptor拦截器拦截,查看是否请求头中包含traceId的字段,若没有就生成,并且放到ThreadLocal中去
     * 2.feign调用时,RequestInterceptor请求拦截器设置请求头,设置traceId
     * 3.接收方,拦截器拦截,从请求头中获取traceId,设置到MDC中去
     * 3.异步线程,使用Spring提供的ThreadPoolTaskExecutor,自己重写适配器,将traceId注入MDC中,保证异步线程的log打印出来也是相同的traceId
     */

//    @LogAspect
 /*   @PostMapping("/test")
    public Result<Object> proLogMethod(@RequestBody JSONObject obj) throws IOException {
        if (obj.getString("flag").equals("1")){
            Result<Object> objectResult = conFeign.logTest();
            System.out.println("body---:"+JSONObject.toJSONString(objectResult));
        }else {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("321","333");
            conFeign2.objtest(jsonObject);
        }
        return new Result<>();
    }
    @GetMapping("/test2")
    public Result<Object> proLogMethod2() throws IOException {
//        conFeign.objtest(new JSONObject(){{put("abc","sz");}});
        return new Result<>();
    }*/
    public static void method(){
        LogThreadLocal.log("abcde666");
    }
}
